import glob
import configparser
import os
import matplotlib.pyplot as plt
import json
import cv2

config = configparser.ConfigParser()
config.read('conf.text')

image_path = config.get('result', 'predict_T51_data_dir')
json_path = config.get('result', 'predict_T51_labels_path')
save_path = config.get('result', 'save_T51_path')
true_coord_path = config.get('result', 'true_coord_path')


def result(image_path):

    current_dir = os.path.realpath(os.curdir)
    # image_path = os.path.join(current_dir, image_path)
    # json_path = os.path.join(current_dir, json_path)
    dcm_paths = glob.glob(os.path.join(image_path, "**", "**.jpg"))
    f = open(json_path, 'r')
    tags = dict(json.load(f))
    f.close()

    ft = open(true_coord_path, 'r') 
    tags_t = dict(json.load(ft))
    ft.close()
    # print(tags)
    i = 0
    for p in dcm_paths:
        print(p)
        img_arr = plt.imread(p)
        size = img_arr.shape
        fig = plt.figure(figsize=(size[1]/100, size[0]/100))
        print(size)
        ax = fig.add_subplot(1, 1, 1)
        index = p.split('/')[-1].split('.')[-2][-4:]

        coords = tags[index]
        for coord in coords:
            # x = coord[0] // (368 / size[1])
            # y = coord[1] // (368 / size[0])
            x, y = coord[0], coord[1]
            top_left_x, top_left_y = x - 7, y - 7
            width, height = 14, 14  # 使目标点在正中间
            rect = plt.Rectangle((top_left_x, top_left_y), width, height, fill=True, edgecolor='red', linewidth=1)
            ax.add_patch(rect)  # https://blog.csdn.net/weixin_43486780/java/article/details/105741382

        # 实际坐标点， 如果是predict，将这段注释掉
        # coords_t = tags_t[index]
        # for coord in coords_t:
        #     # x = coord[0] // (368 / size[1])
        #     # y = coord[1] // (368 / size[0])
        #     x, y = coord[0], coord[1]
        #     top_left_x, top_left_y = x - 3, y - 3
        #     width, height = 6, 6  # 使目标点在正中间
        #     rect_1 = plt.Rectangle((top_left_x, top_left_y), width, height, fill=True, edgecolor='blue', linewidth=1)
        #     ax.add_patch(rect_1)  # https://blog.csdn.net/weixin_43486780/java/article/details/105741382
        
        plt.imshow(img_arr, cmap='gray')
        plt.savefig(save_path + '/' + index + '.jpg')
        plt.show()
    


if __name__ == '__main__':
    result(image_path)
